package boot.spring.service.impl;

import javax.jms.*;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ActiveMQMessageProducer;
import org.apache.activemq.AsyncCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import boot.spring.po.Mail;
import boot.spring.service.Producer;

import java.util.UUID;

@Service("producer")
public class ProducerImpl implements Producer {

    @Autowired
    public JmsTemplate jmsTemplate;
    /**
     * 也可以使用 二次封装的模板 JmsMessagingTemplate
     */
//	@Autowired
//	public JmsMessagingTemplate jmsMessagingTemplate;
    @Autowired
    public ActiveMQConnectionFactory activeMQConnectionFactory;

    @Override
    public void sendMail(Destination des, Mail mail) {
        // 默认签收
        jmsTemplate.convertAndSend(des, mail);
    }

    /**
     * @param des
     * @param mail
     * @throws Exception
     */
    public void send(Destination des, Mail mail) throws Exception {
        Connection connection = activeMQConnectionFactory.createConnection();
        connection.start();
        // 3. 创建session
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        // producer换成ActiveMQMessageProducer
        ActiveMQMessageProducer activeMQMessageProducer = (ActiveMQMessageProducer) session.createProducer(des);
        // 5. 生产消息
        ObjectMessage message = session.createObjectMessage();
        // 给消息设置上id
        message.setJMSMessageID(UUID.randomUUID().toString());
        // 获取自己设置的id
        String messageId = message.getJMSMessageID();
        message.setObject(mail);
        // 6. 使用异步回调方式将消息发送到MQ
        activeMQMessageProducer.send(message, new AsyncCallback() {
            // 发送成功执行这个方法
            @Override
            public void onSuccess() {
                System.out.println("id为：" + messageId + " 的消息发送成功！");
            }
            // 发送失败执行这个方法
            @Override
            public void onException(JMSException e) {
                System.out.println("id为：" + messageId + " 的消息发送失败！");
            }
        });
        // 7. 关闭资源(顺着申请，倒着关闭)
        activeMQMessageProducer.close();
        session.commit();
        session.close();
        connection.close();
        System.out.println("发送到MQ完成！");
    }
    /** 使用事务机制发送消息：JmsListenerContainerFactory bean 设置 .setSessionTransacted(true);
     * @param des
     * @param mail
     * @throws Exception
     */
    public void sendBySession(Destination des, Mail mail) throws Exception {
        Connection connection = activeMQConnectionFactory.createConnection();
        connection.start();
        // 3. 创建session
        Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
        // producer换成ActiveMQMessageProducer
        ActiveMQMessageProducer activeMQMessageProducer = (ActiveMQMessageProducer) session.createProducer(des);
        // 5. 生产消息
        ObjectMessage message = session.createObjectMessage();
        // 给消息设置上id
        message.setJMSMessageID(UUID.randomUUID().toString());
        // 获取自己设置的id
        String messageId = message.getJMSMessageID();
        message.setObject(mail);
        // 6. 使用异步回调方式将消息发送到MQ
        activeMQMessageProducer.send(message);
        //生产者提交事务完成以后，该消息才能存放到队列之中
        session.commit();
        // 7. 关闭资源(顺着申请，倒着关闭)
        activeMQMessageProducer.close();
        session.close();
        connection.close();
        System.out.println("发送到MQ完成！");
    }
}
